package com.yuanfeng.activity.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yuanfeng.activity.entity.ActivityNoneSlideEntity;
import com.yuanfeng.activity.mapper.ActivityNoneSlideMapper;
import com.yuanfeng.activity.service.ActivityNoneSlideService;
import com.yuanfeng.activity.vo.SelfSupportIndexGoodsVO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.goods.DetailModelVO;
import com.yuanfeng.commoms.vo.goods.NoneGoodsQueryDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 *
 */
@Service
public class ActivityNoneSlideServiceImpl extends ServiceImpl<ActivityNoneSlideMapper, ActivityNoneSlideEntity>
    implements ActivityNoneSlideService{

    @Override
    public ResponseResult queryNoneSlide(NoneGoodsQueryDTO param) {
        List<ActivityNoneSlideEntity> slideList = this.baseMapper.selectList(new LambdaQueryWrapper<ActivityNoneSlideEntity>()
                                                 .eq(ActivityNoneSlideEntity::getNoneType,param.getNoneType())
                                                 .orderByDesc(ActivityNoneSlideEntity::getDisplayOrder));
        if (CollectionUtils.isEmpty(slideList)) {
            slideList = Lists.newArrayList();
        }
        slideList.forEach(e -> {
            if (e.getActionType().equals(1) || e.getActionType().equals(3)|| e.getActionType().equals(2)) {
                DetailModelVO model = JSON.parseObject(e.getActionDetail(), DetailModelVO.class);
                if (null != model && e.getActionType().equals(1)) {
                    SelfSupportIndexGoodsVO model2 = this.baseMapper.selectByCommonId(model.getCommonId());
                    if (null == model2) {
                        model2 = new SelfSupportIndexGoodsVO();
                        model2.setCommonId(null);
                    }
                    e.setActionDetailStr(model2);
                } else {
                    e.setActionDetailStr(model);
                }

            } else {
                DetailModelVO model = new DetailModelVO(null);
                model.setCommonId(null);
                e.setActionDetailStr(model);
            }
        });

        return ResponseResult.success(slideList);
    }

    @Override
    public ResponseResult updateSlideNone(NoneGoodsQueryDTO param) {
        Integer actionType = param.getActionType();

        if (null != actionType && (actionType.equals(1) || actionType.equals(3)|| actionType.equals(2))) {
            DetailModelVO model = param.getActionDetail();
            model = dealActionType(actionType, model);
            if (null == model || (null == model.getUrl() && null == model.getCommonId() && null== model.getShopId())) {
                return ResponseResult.fail(BizCodeEnume.MISSING_PARAMETER);
            }
            param.setActionDetailStr(JSON.toJSONString(model));
        } else {
            param.setActionDetailStr(null);
        }
        if (null == param.getId()) {
            this.baseMapper.insertSlideNone(param);
        } else {
            this.baseMapper.updateSlideNone(param);
        }

        return ResponseResult.success(BizCodeEnume.UPDATE_SUCCEEDED);
    }

    @Override
    public ResponseResult deleteNoneSilde(NoneGoodsQueryDTO param) {
        this.baseMapper.deleteById(param.getId());
        return ResponseResult.success(BizCodeEnume.DELETE_SUCCESS);
    }

    private DetailModelVO dealActionType(Integer actionType, DetailModelVO model) {
        DetailModelVO newModel = new DetailModelVO();
        if (actionType.equals(1)) {
            newModel.setCommonId(model.getCommonId());
            newModel.setGoodsId(model.getGoodsId());
        } else if (actionType.equals(2)) {
            newModel.setShopId(model.getShopId());
            newModel.setShopName(model.getShopName());
        } else if (actionType.equals(3)) {
            newModel.setUrl(model.getUrl());
        } else if (actionType.equals(7)) {
            newModel.setNoneType(model.getNoneType());
        }
        return newModel;
    }
}




